package com.yft.dao.base;

import java.io.Serializable;
import java.sql.Timestamp;

import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;

public class DaoInterceptor extends EmptyInterceptor {

	private static final long serialVersionUID = 4340400153619246582L;

	public boolean onSave(Object entity, Serializable id, Object[] state,
			String[] propertyNames, Type[] types) {
		for (int i = 0; i < propertyNames.length; i++) {	
			if ("createTime".equals(propertyNames[i]) || "updateTime".equals(propertyNames[i])) {
				state[i] = new Timestamp(System.currentTimeMillis());
			}
			if ("isDelete".equals(propertyNames[i])){
				state[i] = 0;
			}
		}
		return true;
	}

	public boolean onFlushDirty(Object entity, Serializable id,
			Object[] state, Object[] previousState,
			String[] propertyNames, Type[] types) {
		for (int i = 0; i < propertyNames.length; i++) {
			if ("updateTime".equals(propertyNames[i])) {
				state[i] = new Timestamp(System.currentTimeMillis());
			}
		}
		return true;
	}
}
